home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / demos / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  2.7 KB  |  96 lines

  1. /*
  2.  * Copyright (c) 1996 Silicon Graphics, Inc.
  3.  * 
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee,
  6.  * provided that (i) the above copyright notices and this permission
  7.  * notice appear in all copies of the software and related documentation,
  8.  * and (ii) the name of Silicon Graphics may not be used in any
  9.  * advertising or publicity relating to the software without the specific,
  10.  * prior written permission of Silicon Graphics.
  11.  * 
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  13.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  14.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  15.  * 
  16.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL,
  17.  * INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
  18.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  19.  * OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  20.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  21.  * OF THIS SOFTWARE.
  22.  */
  23. /*
  24.  * Misc utility routines.
  25.  */
  26. #include <string.h>
  27. #include <GL/gl.h>
  28. #include "util.h"
  29.  
  30. /*
  31.  *  Returns true if extension named 'name' is supported.
  32.  */
  33. GLboolean QueryExtension(char *extName)
  34. {
  35.     /*
  36.     ** Search for extName in the extensions string. Use of strstr()
  37.     ** is not sufficient because extension names can be prefixes of
  38.     ** other extension names. Could use strtok() but the constant
  39.     ** string returned by glGetString can be in read-only memory.
  40.     */
  41.     char *p = (char *)glGetString(GL_EXTENSIONS);
  42.     char *end = p + strlen(p);
  43.     while (p < end) {
  44.         int n = strcspn(p, " ");
  45.         if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0)) {
  46.             return GL_TRUE;
  47.         }
  48.         p += (n + 1);
  49.     }
  50.     return GL_FALSE;
  51. }
  52.  
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <unistd.h>
  56. #include <math.h>
  57. /*
  58.  * Misc vector op routines.
  59.  */
  60.  
  61. float x_axis[] = { 1.0, 0.0, 0.0 };
  62. float y_axis[] = { 0.0, 1.0, 0.0 };
  63. float z_axis[] = { 0.0, 0.0, 1.0 };
  64. float nx_axis[] = { -1.0, 0.0, 0.0 };
  65. float ny_axis[] = { 0.0, -1.0, 0.0 };
  66. float nz_axis[] = { 0.0, 0.0, -1.0 };
  67.  
  68. void norm(float v[3])
  69. {
  70.     float r;
  71.  
  72.     r = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  73.  
  74.     v[0] /= r;
  75.     v[1] /= r;
  76.     v[2] /= r;
  77. }
  78.  
  79. float dot(float a[3], float b[3])
  80. {
  81.     return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
  82. }
  83.  
  84. void cross(float v1[3], float v2[3], float result[3])
  85. {
  86.     result[0] = v1[1]*v2[2] - v1[2]*v2[1];
  87.     result[1] = v1[2]*v2[0] - v1[0]*v2[2];
  88.     result[2] = v1[0]*v2[1] - v1[1]*v2[0];
  89. }
  90.  
  91. float length(float v[3])
  92. {
  93.     float r = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  94.     return r;
  95. }
  96.